import {Page} from '@/base/Page';
import {Coupons} from '@/base/model/Coupons';
import {isEmpty} from '@/base/util/Strings';
import {getCouponsList, banCoupon, PutCouponId, deleteCoupon} from '@/base/api/CouponsApi';
import {Agent} from '@/base/model/Agent';
import {Company} from '@/base/model/Company';
import {getAgentList} from '@/base/api/AgentApi';
import {getCompanyList} from '@/base/api/CompanyApi';

class Form {
  CouponName = '';
  createBegin = '';
  createEnd = '';
  date = [];
  state = 0;
}

export class CouponsBloc {
  form = new Form();
  paged: Page<Coupons> = new Page<Coupons>([], 0, 1, 15, 0, []);
  angent: Page<Agent> = new Page<Agent>([], 0, 1, 15, 0, []);
  companyPage: Page<Company> = new Page<Company>([], 0, 1, 15, 0, []);
  loading = false;
  type: string | undefined;
  logintype: string | undefined;
  sortList: any = [];
  companyList: any = [];

  load(pageIndex: number, pageSize: number) {
    if (!isEmpty(this.form.date) && !isEmpty(String(this.form.date[0]))) {
      // @ts-ignore
      this.form.createBegin = this.form.date[0].getTime() || '';
      // @ts-ignore
      this.form.createEnd = this.form.date[1].getTime() + 24 * 60 * 60 * 1000 || '';
    } else {
      this.form.createBegin = '';
      this.form.createEnd = '';
    }
    this.loading = true;
    return getCouponsList({
      pageIndex,
      pageSize,
      sortDirection: 'DESC',
      couponName: this.form.CouponName,
      createBegin: this.form.createBegin,
      createEnd: this.form.createEnd
    }).then((res) => {
      this.paged = res.data;
    }).finally(() => {
      this.loading = false;
    });
  }
  getEndTime(param) {
    let newDate = new Date(param.startTime);
    if (param.daynum) {
      newDate.setDate(newDate.getDate() + param.daynum);
    }
    return newDate;
  }
  compareTime(param) {
    if (!param.startTime) {
      return false;
    }
    // 将 startTime 转换为 Date 对象
    const start = new Date(param.startTime);
    start.setDate(start.getDate() + param.daynum);
    // 生成日期字符串（保持与 startTime 相同的格式）
    const endTime = start.toISOString().split('T')[0];
    const currentDate = new Date().toISOString().split('T')[0];
    // 比较 endTime 和 currentDate
    return new Date(endTime) <= new Date(currentDate);
  }
  switchStatus(id, value) {
    if (value) {
      this.form.state = 1
    } else {
      this.form.state = 0
    }
    this.modifyItem(id)
  }
  modifyItem(
    couponId: number
  ) {
    this.loading = true;
    return PutCouponId({
      couponId
    }).then((res) => {
      this.load(this.paged.pageIndex, this.paged.pageSize);
    }).finally(() => {
      this.loading = false;
    })
  }

  reset() {
    this.form = new Form();
    this.paged.pageIndex = 1;
    this.paged.pageSize = 15;
    this.load(this.paged.pageIndex, this.paged.pageSize);
  }
  removeItem(dateItem: Coupons, sym) {
    this.loading = true;
    if (sym) { // 作废
      return banCoupon({couponId: dateItem.couponId})
        .finally(() => {
          this.loading = false;
        });
    } else {
      return deleteCoupon({couponId: dateItem.couponId})
        .finally(() => {
          this.loading = false;
        });
    }
 }
  getAgentList(pageIndex: number, pageSize: number, sortDirection: string) {
    this.loading = true;
    return getAgentList({
      pageIndex: this.angent.pageIndex,
      pageSize: 1000,
      sortDirection: 'ESC'
    }).then((res) => {
      this.sortList = res.data.items.map(function (item) {
        var obj = {id: 0, agentName: ''};
        // @ts-ignore
        obj.id = item.id;
        obj.agentName = item.agentName;
        return obj;
      });
      return this.sortList;
    }).finally(() => {
      this.loading = false;
    });
  }
  getCompanyList(pageIndex: number, pageSize: number, sortDirection: string) {
    this.loading = true;
    return getCompanyList({
      pageIndex: 1,
      pageSize: 50,
      sortDirection: 'ASC'
    }).then((res) => {
      this.companyList = res.data.items.map(function (item) {
        var obj = {id: 0, name: ''};
        // @ts-ignore
        obj.id = item.id;
        obj.name = item.name;
        return obj;
      });
      return this.companyList;
    }).finally(() => {
      this.loading = false;
    });
  }
  getCompanyName(companyId: number) {
    const item = this.companyList.find(x => x.id === companyId);
    return item == null ? '' : item.name;
  }
  getAgentName(agentId: number) {
    const item = this.sortList.find(x => x.id === agentId);
    return item == null ? '' : item.agentName;
  }
}
